'use strict';

const Controller = require('./base');

class WarrantyController extends Controller {
    async every() {
        const { ctx } = this;
        this.success(await ctx.service.warranty.every());
    }

    //查询当天数据
    async today() {
        const { ctx } = this;
        this.success(await ctx.service.warranty.today());
    }

    // async count() {
    //     const { ctx } = this;
    //     let equWhere = ctx.request.body
    //     this.success(await ctx.service.warranty.count(equWhere));
    // }
    async page() {
        const { ctx } = this;
        const query = ctx.query;
        ctx.validate({
            limit: { type: 'string', required: true },
            page: { type: 'string', required: true },
        }, query);
        this.success(await ctx.service.warranty.page(query));
    }
    async create() {
        const { ctx } = this;
        const body = ctx.request.body;
    
        ctx.validate({
        //   username: { type: 'string', required: true },
        //   name: { type: 'string', required: true },
        //   roles: { type: 'array', required: true },
        }, body);
    
        const res = await ctx.service.warranty.create(body);
        if (res.success) {
          this.success('添加成功');
        } else {
          this.error('添加失败 ' + (res.msg || ''));
        }
      }
    
    async update() {
        const { ctx } = this;
        const body = ctx.request.body;

        ctx.validate({
            num: { type: 'string', required: true },
        }, body);

        const res = await ctx.service.warranty.update(body);
        if (res.success) {
            this.success('修改成功');
        } else {
            this.error('修改失败 ' + (res.msg || ''));
        }
    }

    async delete() {
        const { ctx } = this;
        const body = ctx.request.body;

        ctx.validate({
            num: { type: 'string', required: true },
        }, body)
        const success = await ctx.service.warranty.delete(body);
        if (success) {
            this.success('删除成功');
        } else {
            this.error('删除失败');
        }
    }
    async checkrepair(){
        const {ctx} =this;
        const body = ctx.request.body;
        const success = await ctx.service.warranty.checkrepair(body);
        if (success) {
            this.success('删除成功');
        } else {
            this.error('删除失败');
        }
    }
}
module.exports = WarrantyController;